use Encode;

$x_max = 20;
$y_max = 20;

@ARGV == 1 or die "\n\ttwist.pl filename\n";

$space= decode("gbk"," ");
$ret_line.="  " for(1..$x_max);
$ret_line = decode("gbk",$ret_line);

$col_line.="  " for(1..$y_max);
$col_line = decode("gbk",$col_line);

open IN, "$ARGV[0]" or die"can not open $ARGV[0],$!\n";
undef $/;
$text=<IN>;
close IN;
$text =~ s/\n\n/<SEP1>/sg;
$text =~ s/\n /<SEP>/sg;
$text =~ s/\n//sg;
$text =~ s/<SEP1>/\n\n/sg;
$text =~ s/<SEP>/\n\n /sg;
@text = split("\n",$text);

foreach(@text)
{   
	  chomp;
    @line=split("", decode("gbk",$_) );
    while(@line > $y_max)
    {
        @temp = splice(@line,0,$y_max);
        push @src, join("",@temp);
    }
    @temp=@line;
    push @temp,$space while(@temp<$y_max);
    push @src, join("",@temp);
}

push @src,$col_line while(@src % $x_max != 0);
$counter=0;
while($counter<@src/$x_max)
{
    for $k (0..$y_max-1)
    {
        $line="|";
        for($i=$x_max*($counter+1)-1;$i>=$x_max*$counter;$i--)
        {           
            $ch = substr($src[$i],$k,1);
            if(ord($ch)<127)
            {
                $line.="$ch |";
            }
            else
            {
                $line.="$ch|";
            }
        }
        push @new,$line;
    }
    $counter++;    
    push @new,$ret_line;
}

foreach(@new)
{
    $_ = encode("gbk",$_);
}

print join "\n",@new;
